<template>
    <div class="cates">
        <div class="header padding-24 space-between bg-active">
            <div class="h-back">
                <p class="fs-32 color-f" @click="cancel">取消</p>
            </div>
            <div class="title fs-36 color-f">选择分类</div>
            <div class="h-right">
                <p class="fs-32 color-f" @click="sure">完成</p>
            </div>
        </div>
        <div class="cate-list flex wrap padding-24 ">
            <span class="list-item bg-f color-6 fs-32" :class="{activeButton:checkval.indexOf(item.id)!=-1}" v-for="(item,index) in cataslist" :key="index" @click="check(item)">{{item.video_cate_name}}</span>
        </div>
    </div>
</template>
<script>
export default {
    data(){
        return{
            cataslist:[],
            checkval:[],
            checkcate:[],
        }
    },
    created(){
        this.getcates()
    },
    mounted(){
        var checkedid=sessionStorage.getItem('checkedid') 
        console.log(checkedid)
        if(checkedid==null||checkedid==undefined||checkedid==''){
            
        }else{
            checkedid =checkedid.split(",");
            //console.log(checkedid)
            if(checkedid[0]){
                this.checkval=checkedid
                for(var i=0;i<this.checkval.length;i++){
                    this.checkval[i]=Number(this.checkval[i]) 
                }
            }
        }
        
        //console.log(this.checkval)
    },
    methods:{
        getcates(){
            let url = this.$host1 + `home/dynamic/videoCateList`
            this.$axios.get(url).then(res=>{
                console.log(res)
                let resData=res.data
                if(res.status==200){
                    if(resData.code==200){
                        this.cataslist=resData.data
                    }else{
                        this.cataslist=[]
                    }
                }
            }).catch(() => {
                this.AlertWin("网络错误！请稍后重试！");
            });
        },
        cancel(){
            this.$router.go(-1)
            sessionStorage.removeItem('checkedid')
            sessionStorage.removeItem('checkedcate')
        },
        sure(){
            this.checkcate=[]
            for(var i=0;i<this.checkval.length;i++){
                for(var j=0; j<this.cataslist.length;j++){
                    if(this.checkval[i]==this.cataslist[j].id){
                        this.checkcate.push(this.cataslist[j].video_cate_name)
                    }
                }
            }
            //console.log(this.checkcate)
            sessionStorage.setItem('checkedid',this.checkval)
            sessionStorage.setItem('checkedcate',this.checkcate)
            this.$router.go(-1)
        },
        check(item) {
            var idIndex = this.checkval.indexOf(item.id)      
            if (idIndex >= 0) {
                 this.checkval.splice(idIndex, 1)
            } else {
                if(this.checkval.length<3){
                    this.checkval.push(item.id)
                }else{
                    this.AlertWin("最多只能选择三个分类！");
                }
                       
            }
            //console.log(this.checkval)
        },
    }
}
</script>
<style lang="scss" scoped>
.cates{
    width: 100%;
    //height: 100%;
    //position: absolute;
    padding-top: 1rem;
    box-sizing: border-box;
    //background-color: #fff;
    .header{
        width: 100%;
        height: 1rem;
        
        .title{
            font-size: 0.36rem;
            font-weight: bold;
        }
      
      
    }
    .cate-list{
       // height: calc(100% - 1rem);
        position: absolute;
        padding-top: 0.2rem;
        .list-item{
            display: block;
            width: auto;
            height: 0.8rem;
            padding: 0 0.1rem;
            box-sizing: border-box;
            border: 0.01rem solid #bbb;
            border-radius: 0.08rem;
            text-align: center;
            line-height: 0.82rem;
            margin: 0 0.1rem 0.2rem;
        }
        .activeButton{
            border: 0.01rem solid #F44349;
            color: #F44349;
        }
    }
}
</style>